package payment.simulator.institution.tx.gatheringaccredit;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import payment.api.tx.gatheringaccredit.Tx2731Request;

/**
 * 进件处理接口(2731)
 * 
 * <pre>
 * Modify Information:
 * Author       Date        Description
 * ============ =========== ============================
 * lihao        2019/03/22  Create this file
 * 
 * </pre>
 */
public class Tx2731 extends HttpServlet{

	private static final long serialVersionUID = 7149678584748116366L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            // 1.取得参数
            String institutionID = request.getParameter("InstitutionID");
            String batchNo = request.getParameter("BatchNo");
            String itemNo = request.getParameter("ItemNo");
            String institutionName = request.getParameter("InstitutionName");
            String institutionShortName = request.getParameter("InstitutionShortName");
            String settlementFlag = request.getParameter("SettlementFlag");
            String bankID = request.getParameter("BankID");
            String accountName = request.getParameter("AccountName");
            String accountNumber = request.getParameter("AccountNumber");
            String branchName = request.getParameter("BranchName");
            String province = request.getParameter("Province");
            String city = request.getParameter("City");

            // 2.创建交易请求对象
            Tx2731Request tx2731Request = new Tx2731Request();
            tx2731Request.setInstitutionID(institutionID);
            tx2731Request.setBatchNo(batchNo);
            tx2731Request.setItemNo(itemNo);
            tx2731Request.setInstitutionName(institutionName);
            tx2731Request.setInstitutionShortName(institutionShortName);
            tx2731Request.setSettlementFlag(settlementFlag);
            tx2731Request.setBankID(bankID);
            tx2731Request.setAccountName(accountName);
            tx2731Request.setAccountNumber(accountNumber);
            tx2731Request.setBranchName(branchName);
            tx2731Request.setProvince(province);
            tx2731Request.setCity(city);
            
            // 3.执行报文处理
            tx2731Request.process();

            // 4.将参数放置到request对象
            request.setAttribute("plainText", tx2731Request.getRequestPlainText());
            request.setAttribute("message", tx2731Request.getRequestMessage());
            request.setAttribute("signature", tx2731Request.getRequestSignature());
            request.setAttribute("txCode", "2731");
            request.setAttribute("txName", "进件处理接口");
            request.setAttribute("action", request.getContextPath() + "/SendMessage");

            // 5.转向Request.jsp页面
            request.getRequestDispatcher("/Request.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
            processException(request, response, e.getMessage());
        }
    }

    private void processException(HttpServletRequest request, HttpServletResponse response, String errorMessage) {
        try {
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
        } catch (Exception e) {
        }
    }
}

